package cn.tedu.egvehicle1.controller;

import cn.tedu.egvehicle1.base.response.JsonResult;
import cn.tedu.egvehicle1.base.response.StatusCode;
import cn.tedu.egvehicle1.mapper.VehicleMapper;
import cn.tedu.egvehicle1.pojo.dto.VehicleAddParam;
import cn.tedu.egvehicle1.pojo.dto.VehicleListQuery;
import cn.tedu.egvehicle1.pojo.entity.Vehicle;
import cn.tedu.egvehicle1.pojo.vo.UserVO;
import cn.tedu.egvehicle1.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/vehicle/")
@Slf4j
@Api("02.车辆管理模块")
public class VehicleController {
    @Autowired
    public VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam,
                          @ApiIgnore HttpSession session){
        log.debug("vehicleAddParam={}",vehicleAddParam);

         UserVO userVO = (UserVO) session.getAttribute("user");
         if(userVO == null){
             return new JsonResult(StatusCode.NOT_LOGIN);
         }

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());

        log.debug("vehicle={}",vehicle);

        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.success();
    }

    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(@RequestBody VehicleListQuery vehicleListQuery,
                           @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery={}",vehicleListQuery);

        UserVO userVO =(UserVO) session.getAttribute("user");
        if(userVO == null ){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userid = userVO.getId();

        List<VehicleListVO>  vehicleListVOS = vehicleMapper.selectVehicle(brand,license,userid);

        return JsonResult.success(vehicleListVOS);
    }

    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id",value = "车辆编号",required = true,type = "Long")
    public JsonResult del(@Range(min = 1,max = 100,message = "车辆ID必须在1到100之间")@PathVariable Long id, @ApiIgnore HttpSession session){
        log.debug("id = "+id);

        if(id<0){
            throw new IllegalArgumentException("车辆ID不能小于0");
        }

        UserVO userVO =(UserVO) session.getAttribute("user");
        if(userVO == null ){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        int num = vehicleMapper.delectVehicle(id);
        if(num>0){
            return JsonResult.success();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED,"该车辆不存在");
        }


    }
}
